package com.car.rental.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.rental.common.core.response.ResponseResult;
import com.car.rental.order.dto.OrderCreateDTO;
import com.car.rental.order.service.OrderService;
import com.car.rental.order.vo.OrderDetailVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @PostMapping
    public ResponseResult<String> createOrder(
            @RequestAttribute("userId") Long userId,
            @RequestBody @Valid OrderCreateDTO createDTO) {
        return orderService.createOrder(userId, createDTO);
    }

    @PostMapping("/{orderNo}/cancel")
    public ResponseResult<Void> cancelOrder(
            @RequestAttribute("userId") Long userId,
            @PathVariable String orderNo) {
        return orderService.cancelOrder(userId, orderNo);
    }

    @GetMapping("/{orderNo}")
    public ResponseResult<OrderDetailVO> getOrderDetail(
            @RequestAttribute(value = "userId", required = false) Long userId,
            @PathVariable String orderNo) {
        if (userId == null) {
            return orderService.getOrderDetailNoAuth(orderNo);
        }
        return orderService.getOrderDetail(userId, orderNo);
    }

    @GetMapping("/list")
    public ResponseResult<Page<OrderDetailVO>> getUserOrders(
            @RequestAttribute("userId") Long userId,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return orderService.getUserOrders(userId, status, current, size);
    }

    @PostMapping("/{orderNo}/complete")
    public ResponseResult<Void> completeOrder(
            @RequestHeader("userId") Long userId,
            @PathVariable String orderNo) {
        return orderService.completeOrder(userId, orderNo);
    }

    @PostMapping("/{orderNo}/pickup")
    public ResponseResult<Void> pickupVehicle(
            @RequestAttribute("userId") Long userId,
            @PathVariable String orderNo) {
        return orderService.pickupVehicle(userId, orderNo);
    }

    @PostMapping("/{orderNo}/return")
    public ResponseResult<Void> returnVehicle(
            @RequestAttribute("userId") Long userId,
            @PathVariable String orderNo) {
        return orderService.returnVehicle(userId, orderNo);
    }
} 